package com.yc.admin.controller.common;

import com.yc.common.core.domain.AjaxResult;
import com.yc.common.core.domain.model.RegisterBody;
import com.yc.common.utils.StringUtil;
import com.yc.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@Api("注册")
public class RegisterController {

    @Resource
    private ISysUserService sysUserService;


    /**
     * 用户信息注册
     * @param registerBody
     * @return
     */
    @PostMapping("/register")
    @ApiOperation("用户注册")
    public AjaxResult register(@RequestBody RegisterBody registerBody) {

        AjaxResult ajax = AjaxResult.success();

        String password = registerBody.getPassword();
        String rePassword = registerBody.getRePassword();
        String nickName = registerBody.getNickName();

        if (nickName == null || StringUtil.isEmpty(nickName)) {
            return AjaxResult.error("用户名不能为空！");
        }

        if (null == password || StringUtil.isEmpty(password) || !password.equals(rePassword)) {
            return AjaxResult.error("密码不一致，请重新输入！");
        }

        boolean flag = sysUserService.addUserInfo(registerBody);

        if (flag) {
            log.info("注册成功");
            return ajax.put("msg","注册成功，3秒后返回登录");
        } else {
            return AjaxResult.error("注册失败，请检查用户名和密码");
        }

    }

}
